#!/bin/sh
FILES=$(git diff --cached --name-only --diff-filter=ACMR "*.swift" | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0

GIT_ROOT=$(git rev-parse --show-toplevel)

# Bazel invocation may git clone some repositories, and override these env vars.

_GIT_INDEX_FILE=$GIT_INDEX_FILE

unset GIT_INDEX_FILE

# Prettify all selected files
export DYLD_FALLBACK_LIBRARY_PATH=$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx
echo "$FILES" | xargs -I {} bazel run --compilation_mode=opt @SwiftFormat//:swift-format -- format --configuration "$GIT_ROOT/.swift-format.json" -i "$GIT_ROOT/{}"

export GIT_INDEX_FILE=$_GIT_INDEX_FILE

# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add

exit 0

